package com.feihao.zhou.controller;

import com.feihao.zhou.controller.results.Code;
import com.feihao.zhou.controller.results.Result;
import com.feihao.zhou.domain.User;
import com.feihao.zhou.service.UserService;
import com.feihao.zhou.system.exception.BusinessException;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    @ResponseBody
    public Result save(User user) {
        try {
            boolean flag = userService.save(user);
            return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("添加失败啦", Code.SAVE_ERROR);
        }
    }

    @PutMapping
    @ResponseBody
    public Result update(User user) {
        try {
            boolean flag = userService.update(user);
            return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("修改失败！", Code.UPDATE_ERROR);
        }
    }

    @DeleteMapping("/{uuid}")
    @ResponseBody
    public Result delete(@PathVariable Integer uuid) {
        try {
            boolean flag = userService.delete(uuid);
            return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("删除失败", Code.DELETE_ERROR);
        }


        //测试用：
        /*if (true){
            throw new BusinessException("删除失败",Code.DELETE_ERROR);
        }
        return  new Result(true ? Code.DELETE_OK : Code.DELETE_ERROR);*/
    }

    @GetMapping("/{uuid}")
    @ResponseBody
    public Result get(@PathVariable Integer uuid) {
        try {
            User user = userService.get(uuid);
            return new Result(null != user ? Code.GET_OK : Code.GET_ERROR, user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("查询失败",Code.GET_ERROR);
        }
    }

    @GetMapping("/{page}/{size}")
    @ResponseBody
    public Result getAll(@PathVariable Integer page, @PathVariable Integer size) {
        try {
            PageInfo<User> all = userService.getAll(page, size);
            return new Result(null != all ? Code.GET_OK : Code.GET_ERROR, all);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("查询失败",Code.GET_ERROR);
        }

    }

    @PostMapping("/login")
    @ResponseBody
    public Result login(String userName, String password) {
        try {
            User user = userService.login(userName, password);
            return new Result(null != user ? Code.GET_OK : Code.GET_ERROR, user);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("登录失败",00000);
        }
    }

}

